/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.article;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.article.PriceDetails;

/**
 *
 * @author Joris
 */
@Stateless
public class PriceFacade extends AbstractFacade<Price> implements PriceFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;
    
    @EJB
    private Size1FacadeLocal sizeFacade;
    
    @EJB 
    private ModelFacadeLocal modelFacade;
    
    @EJB
    private PriceclassFacadeLocal priceClasseFacade;
    
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PriceFacade() {
        super(Price.class);
    }
    
    @Override
    public Price createPrice(PriceDetails pd, Article a, Size1 size, Model m, Priceclass pc){
        Price price = new Price();
        price.setCoefficient(pd.getCoefficient());
        price.setDiscount(pd.getDiscount());
        price.setPrice(pd.getPrice());
        price.setPurchase(pd.getPurchase());
        price.setArticleid(a);
        price.setSizeid(size);
        price.setModelid(m);
        price.setPriceClassid(pc);
        create(price);
        return price;
    }
    
    @Override
    public PriceDetails convert (Price p) {
        return new PriceDetails(p.getId(), p.getPurchase(), p.getCoefficient(),
                p.getDiscount(), p.getPrice(), p.getUpdated(), p.getCreated(),
                modelFacade.convert(p.getModelid()),
                sizeFacade.convert(p.getSizeid()), priceClasseFacade.convert(p.getPriceClassid()));
    }
    
    @Override
    public Collection<PriceDetails> convert (Collection<Price> prices){
        Collection<PriceDetails> details = new ArrayList<PriceDetails>();
        for(Price price : prices){
            details.add(convert(price));
        }
        return details;
    }
    
}
